using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Silvery
{
    /// <summary>
    /// 可以UpData
    /// </summary>
    public interface ICanUpData
    {
        void OnUpdata(BaseObject sender, ITimer timer);
    }
    /// <summary>
    /// 动态物体的接口
    /// </summary>
    public interface IDynamicObject : ICanUpData
    {
        DispatcherTimer SelfLoop { get; }
        TimeSpan SelfLoopInterval { get; set; }
        bool IsSelfLoopRun { get; set; }        
    }
    /// <summary>
    /// 动态物体
    /// 相比和可移动物体而言动态物体拥有了自身循环
    /// </summary>
    public abstract class DynamicObject : BaseObject, IDynamicObject
    {
        /// <summary>
        /// 自身循环的DispatcherTimer
        /// </summary>
        public DispatcherTimer SelfLoop { get; private set; }
        public DynamicObject()
        {
            SelfLoopInterval = TimeSpan.FromMilliseconds(33);
        }
        
        /// <summary>
        /// 自身循环是否运行
        /// </summary>
        public bool IsSelfLoopRun
        {
            get { return SelfLoop == null ? false : true; }
            set
            {
                if (IsSelfLoopRun == value)
                    return;

                if (value)
                {
                    SelfLoop = new DispatcherTimer();
                    SelfLoop.Tick += new EventHandler(selfLoop_Tick);
                    SelfLoopInterval = SelfLoopInterval;
                    SelfLoop.Start();
                }
                else
                {
                    SelfLoop.Stop();
                    SelfLoop.Interval = TimeSpan.Zero;
                    SelfLoop.Tick -= selfLoop_Tick;
                    SelfLoop = null;

                }
                
            }
        }
        TimeSpan selfLoopInterval;
        /// <summary>
        /// 每次自身循环之间的间隔
        /// </summary>
        public TimeSpan SelfLoopInterval
        {
            get { return selfLoopInterval; }
            set
            {
                if(SelfLoop!=null)
                    SelfLoop.Interval = value;
                selfLoopInterval = value;
            }
        }
        void selfLoop_Tick(object sender, EventArgs e)
        {            
            OnUpdata(this, this.ObjectTimer);
            ObjectTimer.Call();
        }
        /// <summary>
        /// 等同于一般游戏的OnUpdata
        /// 默认间隔为33毫秒
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="timer"></param>
        public virtual void OnUpdata(BaseObject sender, ITimer timer)
        {
        }
        /// <summary>
        /// 销毁
        /// </summary>
        public override void Dispose()
        {
            IsSelfLoopRun = false;
            base.Dispose();
        }

    }
}
